# 导入MySQL驱动:
import mysql.connector
# 注意把password设为你的root口令:
conn = mysql.connector.connect(host='localhost', user='root', password='123456', database='heima')
cursor = conn.cursor()
cursor.execute('create table user (id varchar(20) primary key, name varchar(20))')
cursor.execute('insert into user (id, name) values (%s, %s)', ['1', 'Michael'])
print(cursor.rowcount)
# 执行INSERT等操作后要调用commit()提交事务；
conn.commit()
cursor.close()

cursor = conn.cursor()
# MySQL的SQL占位符是%s。
cursor.execute('select * from user where id = %s', ['1'])
values = cursor.fetchall()
print(values)
cursor.close()
conn.close()

# 由于Python的DB-API定义都是通用的，所以，操作MySQL的数据库代码和SQLite类似。

